package fr.cephb.joperon.core.db;


import java.io.File;

import com.sleepycat.db.DatabaseException;


import fr.cephb.joperon.core.Operon;
import fr.cephb.joperon.core.db.base.BaseITermsDB;
import fr.cephb.joperon.core.entities.ITerms;
import fr.cephb.util.Pair;

/**
 * Concrete implementation of BaseITermsDB
 * @author lindenb
 *
 */
public class ITermsDB extends BaseITermsDB
	{
	
	public ITermsDB(Operon operon)
		{
		super(operon);
		}
	
	/**
	 * shortcuts fo getDefinition(id,null);
	 * @param id
	 * @return
	 * @throws DatabaseException
	 */
	public String getDefinition(Integer id)
	throws DatabaseException
		{	
		return getDefinition(id,null);
		}
	
	
	/**
	 * find the definition of a given term id
	 * shortcut to <pre>ITerms t= get(id);
		return t==null?defaultValue:t.getStr();</pre>
	 * @param id
	 * @param defaultValue
	 * @return null if id was not found
	 * @throws DatabaseException
	 */
	public String getDefinition(Integer id,String defaultValue)
	throws DatabaseException
		{	
		if(id==null) return defaultValue;
		ITerms t= get(id);
		return t==null?defaultValue:t.getStr();
		}
	
	/*
	 * finds a term-id from a query
	 * @param tid the term-id to search
	 * @return the identifier of the tid or null if it was not found
	 */
	/* public String getTermName(long tid) throws DatabaseException
		{
		ITerms term= get(tid);
		return term==null?null:term.getStr();
		}*/
	



	
	
	public static void main(String[] args) {
			try {
				Operon op = Operon.newInstance(new File("/operon2/bdb"));
				ITermsDB db= new ITermsDB(op);
				db.open();
				int i=0;
				ITermsDB.KeyValueIterator iter=db.listKeyValues();
				while(iter.hasNext())
					{
					Pair<Integer,ITerms> p=iter.next();
					//if(p.first().length()==0) continue;
					System.err.println("["+p.first()+"] "+p.second());
					if(++i==500) break;
					}
				iter.close();
				
				db.close();
				op.close();
			} catch (Exception e) {
			e.printStackTrace();
			}
			
		}

}
